﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    /*  A JobApplication submitted by a Jobseeker for a Job.    */
    public class DBJobApplication
    {
        //create
        public static bool AddJobApplication(JobApplication jApp, Job job)
        {
            jApp.JobApplicationID = "tempvalue";
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(job);
                    job.JobApplications.Add(jApp);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //retrieve job
        public static JobApplication RetrieveJobApplication(string retrieveID)
        {
            JobApplication application = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                    application =
                    (from j in db.JobApplications
                     where j.JobApplicationID == retrieveID
                     select j).SingleOrDefault();
            }
            return application;
        }
        public static List<JobApplication> RetrieveJobApplicationsByJobId(string jobId)
        {
            using (hireddbEntities db = new hireddbEntities()) {
                var results =
                    from j in db.JobApplications
                    where j.JobID == jobId
                    select j;
                return results.ToList<JobApplication>();
            }
        }
        public static JobApplication RetrieveJobApplicationByUsername(string username)
        {
            JobApplication application = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                application =
                (from j in db.JobApplications
                 where j.JobseekerAccount.Username == username
                 select j).SingleOrDefault();
            }
            return application;
        }
        public static List<JobApplication> RetrieveJobApplicationByCompany(string companyid)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from j in db.JobApplications.Include("Job")
                     where j.Job.CompanyAccount.CAccountID == companyid
                     select j).ToList<JobApplication>();
            }
        
        }

        //update job
        public static bool UpdateJobApplication(JobApplication jApp)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobApplication applicationToUpdate = RetrieveJobApplication(jApp.JobApplicationID);
                    db.Attach(applicationToUpdate);
                    applicationToUpdate.Contents = jApp.Contents;
                    applicationToUpdate.ResumeAddress = jApp.ResumeAddress;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //delete job
        public static bool DeleteJobApplication(JobApplication jApp)
        {
            try
            {
                JobApplication jobToDelete = RetrieveJobApplication(jApp.JobApplicationID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(jobToDelete);
                    db.DeleteObject(jobToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteJobApplicationById(string id)
        {
            try
            {
                JobApplication jobToDelete = RetrieveJobApplication(id);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(jobToDelete);
                    db.DeleteObject(jobToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}